package com.ruoyi.system.client.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.client.C5ApiClientService;
import com.ruoyi.system.domain.c5.GetSteamInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
@Slf4j
public class C5ApiClientServiceImpl implements C5ApiClientService {
    // 请求地址
    private static final String BASE_URL = "https://openapi.c5game.com";
    // 主机
    private static final String HOST = "openapi.c5game.com";

    @Override
    public GetSteamInfoResponse getSteamInfoByC5AppKey(String appKey) {
        if (ObjectUtils.isEmpty(appKey)) {
            throw new RuntimeException("appKey不能为空");
        }
        String response = HttpUtils.sendGet(BASE_URL + "/merchant/account/v1/steamInfo?app-key=" + appKey, null, MediaType.APPLICATION_JSON_VALUE, callback -> {
            callback.setRequestProperty("app-key", appKey);
            callback.setRequestProperty("Host", HOST);
        });

        GetSteamInfoResponse getSteamInfoResponse = JSONObject.parseObject(response, GetSteamInfoResponse.class);
        if (!getSteamInfoResponse.getSuccess()) {
            throw new RuntimeException(getSteamInfoResponse.getErrorMsg());
        }
        return getSteamInfoResponse;
    }
}
